package com.qiaofang.collectmanagebff.facade.impl;

import com.qiaofang.collectmanage.stub.bean.SearchCollectRulePageDTO;
import com.qiaofang.collectmanage.stub.bean.SearchCollectRulePostDTO;
import com.qiaofang.collectmanage.stub.constants.CollectRuleResponseCode;
import com.qiaofang.collectmanage.stub.request.GetCitysRequest;
import com.qiaofang.collectmanage.stub.request.GetCollectRuleRequest;
import com.qiaofang.collectmanage.stub.request.GetSourcesRequest;
import com.qiaofang.collectmanage.stub.response.GetCitysResponse;
import com.qiaofang.collectmanage.stub.response.GetCollectRuleResponse;
import com.qiaofang.collectmanage.stub.response.GetSourcesResponse;
import com.qiaofang.collectmanagebff.base.vo.CollectRuleVO;
import com.qiaofang.collectmanagebff.facade.request.*;
import com.qiaofang.collectmanagebff.facade.response.*;
import com.qiaofang.collectmanagebff.service.CollectManageSampleService;
import com.qiaofang.collectmanagebff.service.CollectRuleService;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.response.CommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/collectrule")
@Api(value = "采集规则集合",tags="collectrule")
public class CollectRuleFacadeImplBff {
	@Autowired
	private CollectRuleService collectRuleService;

	@RequestMapping(value = "/getCollectRuleById", method = RequestMethod.POST)
	@ApiOperation(value = "查询对象根据id")
	public GetCollectRuleByIdBffResponse getCollectRuleById(@RequestBody @Valid GetCollectRuleByIdBffRequest request,BindingResult bindingResult) {
		GetCollectRuleByIdBffResponse response = new GetCollectRuleByIdBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		response.setData(collectRuleService.getCollectRuleById(request.getId()));
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@RequestMapping(value = "/getCollectRuleByCollectRule", method = RequestMethod.POST)
	@ApiOperation(value = "查询对象根据对象", notes = "查询对象根据对象")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "GetCollectRuleBffRequest") })
	public GetCollectRuleBffResponse getCollectRule(@RequestBody @Valid GetCollectRuleBffRequest request,BindingResult bindingResult) {
		GetCollectRuleBffResponse response = new GetCollectRuleBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		CollectRuleVO collectRuleDTO  = collectRuleService.getCollectRule(request.getGetCollectRuleDTO());
		response.setData(collectRuleDTO);
		return response;
	}


	@RequestMapping(value = "/saveCollectRule", method = RequestMethod.POST)
	@ApiOperation(value = "保存采集规则",notes = "保存采集规则")
	@ApiImplicitParams({@ApiImplicitParam(name="request",value="请求参数",dataType = "SaveCollectRuleBffRequest")})
	public SaveCollectRuleBffResponse saveCollectRule(@RequestBody @Valid SaveCollectRuleBffRequest request,BindingResult bindingResult){
        SaveCollectRuleBffResponse response = new SaveCollectRuleBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
        collectRuleService.saveCollectRule(request.getSaveCollectRuleDTO());
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setData(true);
		return response;
	}

	@RequestMapping(value = "/updateCollectRule", method = RequestMethod.POST)
	@ApiOperation(value = "更新采集规则", notes = "更新采集规则")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "UpdateCollectRuleBffRequest") })
	public UpdateCollectRuleBffResponse updateCollectRule(@RequestBody @Valid UpdateCollectRuleBffRequest request,BindingResult bindingResult) {
		UpdateCollectRuleBffResponse response = new UpdateCollectRuleBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		collectRuleService.updateCollectRule(request.getUpdateCollectRuleDTO());
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setData(true);
		return response;
	}


	@RequestMapping(value = "/removeCollectRule", method = RequestMethod.POST)
	@ApiOperation(value = "删除采集规则", notes = "删除采集规则")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "RemoveCollectRuleBffRequest") })
	public RemoveCollectRuleBffResponse removeCollectRule(@RequestBody @Valid RemoveCollectRuleBffRequest request,BindingResult bindingResult){
        RemoveCollectRuleBffResponse response = new RemoveCollectRuleBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
        collectRuleService.removeCollectRule(request.getRemoveCollectRuleDTO());
		if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
			throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
		}
		response.setData(true);
		return response;
	}


	@RequestMapping(value = "/searchCollectRulePage", method = RequestMethod.POST)
	@ApiOperation(value = "查询多个对象不包含删的分页列表", notes = "获取多个对象不包含删的分页列表")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "SearchCollectRulePageBffRequest") })
	public SearchCollectRulePageBffResponse searchCollectRulePage(@RequestBody @Valid  SearchCollectRulePageBffRequest request,BindingResult bindingResult){
		SearchCollectRulePageBffResponse response = new SearchCollectRulePageBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		PageDTO pageDTO = new PageDTO();
        SearchCollectRulePostDTO searchCollectRulePostDTO = new SearchCollectRulePostDTO();
        pageDTO.setPageSize(request.getPageSize());
        pageDTO.setPageNum(request.getPageNum());
        searchCollectRulePostDTO.setBelonger(request.getBelonger());
        searchCollectRulePostDTO.setCity(request.getCity());
        searchCollectRulePostDTO.setCollectHouseType(request.getCollectHouseType());
        searchCollectRulePostDTO.setCollectType(request.getCollectType());
        searchCollectRulePostDTO.setSource(request.getSource());
		response.setData(collectRuleService.searchCollectRulePage(pageDTO,searchCollectRulePostDTO));
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@RequestMapping(value = "/commonCityExtend", method = RequestMethod.POST)
	@ApiOperation(value = "对通用网站扩展城市", notes = "对通用网站扩展城市")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "CommonCityExtendBffRequest") })
	public CommonCityExtendBffResponse commonCityExtend(@RequestBody @Valid CommonCityExtendBffRequest request,BindingResult bindingResult){
		CommonCityExtendBffResponse response = new CommonCityExtendBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		collectRuleService.commonCityExtend(request.getCity(),request.getSource(),request.getBelonger(),request.getCollectType(),request.getCollectHouseType(),request.getData());
		response.setData(true);
		return response;
	}


	@PostMapping(value = "getCitys")
	@ApiOperation(value = "获取所有城市", notes = "获取所有城市")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "GetCitysBffRequest") })
	public GetCitysBffResponse getCitys(@RequestBody @Valid GetCitysBffRequest request,BindingResult bindingResult) {
		GetCitysBffResponse response = new GetCitysBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		response.setData(collectRuleService.getCitys());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@PostMapping(value = "getSources")
	@ApiOperation(value = "获取所有采集网站", notes = "获取所有采集网站")
	@ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "GetSourcesBffRequest") })
	public GetSourcesBffResponse getSources(@RequestBody @Valid GetSourcesBffRequest request,BindingResult bindingResult) {
		GetSourcesBffResponse response = new GetSourcesBffResponse();
		if (bindingResult.hasErrors()) {
			String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
			response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
			response.setResponseMessage(defaultMessage);
			return response;
		}
		response.setData(collectRuleService.getSources());
		return response;
	}


}

